package com.maegul.wui;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;

import com.maegul.dao.entities.MediaItem;
import com.maegul.wui.pages.Index;

public class MaegulApplication extends WebApplication {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	@Override
	public Class<? extends Page> getHomePage() {
		return Index.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.wicket.protocol.http.WebApplication#init()
	 */
	@Override
	protected void init() {
		getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.wicket.protocol.http.WebApplication#newSession(org.apache.
	 * wicket.request.Request, org.apache.wicket.request.Response)
	 */
	@Override
	public Session newSession(Request request, Response response) {
		return new MaegulSession(request);
	}

	/**
	 * Da una lista de MediaItem por tipo
	 * 
	 * @param type
	 *            el tipo del que se desea la lista
	 * @return la lista con los elementos que se encontraron dados por el tipo
	 *         especificado
	 * 
	 * @see com.maegul.dao.entities.MediaItem#MUSIC
	 * @see com.maegul.dao.entities.MediaItem#SERIES
	 * @see com.maegul.dao.entities.MediaItem#MOVIES
	 * @see com.maegul.dao.entities.MediaItem#BOOKS
	 */
	public List<MediaItem> getItems(int type) {
		// TODO obtener los items por tipo de la base de datos

		List<MediaItem> items = new ArrayList<MediaItem>();

		try {
			MediaItem rimshot = new MediaItem(
					0,
					"rimshot",
					"Nunc tempor sagittis metus. Nulla facilisi. Morbi"
							+ "tempus gravida egestas. Phasellus nec sem consequat libero semper"
							+ "pulvinar. Aliquam nunc odio, semper sit amet gravida eget,"
							+ "viverra sed quam. Etiam eu sem mauris, vel semper dolor. Ut"
							+ "dictum risus ac purus pharetra eleifend. Donec varius, est at"
							+ "laoreet porta, eros felis commodo leo, non vulputate erat eros et"
							+ "justo. Suspendisse scelerisque urna ac odio hendrerit ultrices."
							+ "Cum sociis natoque penatibus et magnis dis parturient montes,"
							+ "nascetur ridiculus mus. Quisque tempor, risus eget varius"
							+ "imperdiet, nisi nulla pharetra nunc, sit amet hendrerit lacus est"
							+ "id ipsum. Sed rutrum turpis in ligula semper lobortis.",
					500, MediaItem.MUSIC,
					"http://dl.dropbox.com/u/12940330/rimshot.wav");
			MediaItem loser = new MediaItem(
					1,
					"loser",
					"Nunc tempor sagittis metus. Nulla facilisi. Morbi"
							+ "tempus gravida egestas. Phasellus nec sem consequat libero semper"
							+ "pulvinar. Aliquam nunc odio, semper sit amet gravida eget,"
							+ "viverra sed quam. Etiam eu sem mauris, vel semper dolor. Ut"
							+ "dictum risus ac purus pharetra eleifend. Donec varius, est at"
							+ "laoreet porta, eros felis commodo leo, non vulputate erat eros et"
							+ "justo. Suspendisse scelerisque urna ac odio hendrerit ultrices."
							+ "Cum sociis natoque penatibus et magnis dis parturient montes,"
							+ "nascetur ridiculus mus. Quisque tempor, risus eget varius"
							+ "imperdiet, nisi nulla pharetra nunc, sit amet hendrerit lacus est"
							+ "id ipsum. Sed rutrum turpis in ligula semper lobortis.",
					600, MediaItem.MUSIC,
					"http://dl.dropbox.com/u/12940330/loser.wav");
			MediaItem dizzy = new MediaItem(
					2,
					"dizzy",
					"Nunc tempor sagittis metus. Nulla facilisi. Morbi"
							+ "tempus gravida egestas. Phasellus nec sem consequat libero semper"
							+ "pulvinar. Aliquam nunc odio, semper sit amet gravida eget,"
							+ "viverra sed quam. Etiam eu sem mauris, vel semper dolor. Ut"
							+ "dictum risus ac purus pharetra eleifend. Donec varius, est at"
							+ "laoreet porta, eros felis commodo leo, non vulputate erat eros et"
							+ "justo. Suspendisse scelerisque urna ac odio hendrerit ultrices."
							+ "Cum sociis natoque penatibus et magnis dis parturient montes,"
							+ "nascetur ridiculus mus. Quisque tempor, risus eget varius"
							+ "imperdiet, nisi nulla pharetra nunc, sit amet hendrerit lacus est"
							+ "id ipsum. Sed rutrum turpis in ligula semper lobortis.",
					1000, MediaItem.MOVIES,
					"http://dl.dropbox.com/u/12940330/dizzy.ogv");
			
			items.add(rimshot);
			items.add(loser);
			items.add(dizzy);

		} catch (Exception e) {
			e.printStackTrace();
		}

		List<MediaItem> nItems = new ArrayList<MediaItem>();
		switch (type) {
		case MediaItem.MUSIC:
			for (MediaItem mediaItem : items) {
				if (mediaItem.getType() == MediaItem.MUSIC)
					nItems.add(mediaItem);
			}
			return nItems;
		case MediaItem.SERIES:
			for (MediaItem mediaItem : items) {
				if (mediaItem.getType() == MediaItem.SERIES)
					nItems.add(mediaItem);
			}
			return nItems;
		case MediaItem.MOVIES:
			for (MediaItem mediaItem : items) {
				if (mediaItem.getType() == MediaItem.MOVIES)
					nItems.add(mediaItem);
			}
			return nItems;
		case MediaItem.BOOKS:
			for (MediaItem mediaItem : items) {
				if (mediaItem.getType() == MediaItem.BOOKS)
					nItems.add(mediaItem);
			}
			return nItems;
		default:	
			return items;

		}
	}

	public MediaItem getItem(long id) {
		MediaItem it = null;
		for (MediaItem i : getItems(-1)) {
			if (i.getId() == id)
				it = i;
		}
		return it;
	}
}
